package org.example.me.controller;

import org.example.me.entity.ExcelLog;
import org.example.me.enums.ExcelTypeEnum;
import org.example.me.queue.ExcelExportQueue;
import org.example.me.service.ExcelExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * Excel导出测试控制器
 * 提供测试Excel导出功能的REST API接口
 */
@RestController
public class TestController {

    /**
     * Excel导出工厂类，用于根据类型选择对应的导出处理器
     */
    @Autowired
    private ExcelExportService excelExportService;

    /**
     * Excel导出队列，用于异步处理导出任务
     */
    @Autowired
    private ExcelExportQueue excelExportQueue;

    /**
     * 用户数据导出接口
     * 测试用户数据导出功能
     */
    @GetMapping("/exportUser")
    public void exportUser() {
        ExcelLog excelLog = createExcelLog(ExcelTypeEnum.USER);
        excelExportService.export(excelLog);
        System.out.println(excelLog);
    }

    /**
     * 订单数据导出接口
     * 测试订单数据导出功能
     */
    @GetMapping("/exportOrder")
    public void exportOrder() {
        ExcelLog excelLog = createExcelLog(ExcelTypeEnum.ORDER);
        excelExportService.export(excelLog);
        System.out.println(excelLog);
    }

    /**
     * 多Sheet导出接口
     * 测试多Sheet导出功能
     */
    @GetMapping("/exportMultiSheet")
    public void exportMultiSheet() {
        ExcelLog excelLog = createExcelLog(ExcelTypeEnum.MULTI_SHEET);
        excelExportService.export(excelLog);
        System.out.println(excelLog);
    }

    /**
     * 多文件导出接口
     * 测试多文件导出功能
     */
    @GetMapping("/exportMultiFile")
    public void exportMultiFile() {
        ExcelLog excelLog = createExcelLog(ExcelTypeEnum.MULTI_FILE);
        excelExportService.export(excelLog);
        System.out.println(excelLog);
    }

    /**
     * 动态表头导出接口
     * 测试动态表头导出功能
     */
    @GetMapping("/exportDynamicHead")
    public void exportDynamicHead() {
        ExcelLog excelLog = createExcelLog(ExcelTypeEnum.DYNAMIC_HEAD);
        excelExportService.export(excelLog);
        System.out.println(excelLog);
    }

    /**
     * 批量添加异步导出任务接口
     * 一次性添加多种类型的导出任务到队列中
     */
    @GetMapping("/batchAsyncExport")
    public void batchAsyncExport() {
        // 添加用户数据导出任务
        ExcelLog userLog = createExcelLog(ExcelTypeEnum.USER);
        excelExportQueue.addExportTask(userLog);
        // 添加订单数据导出任务
        ExcelLog orderLog = createExcelLog(ExcelTypeEnum.ORDER);
        excelExportQueue.addExportTask(orderLog);
        // 添加多Sheet导出任务
        ExcelLog multiSheetLog = createExcelLog(ExcelTypeEnum.MULTI_SHEET);
        excelExportQueue.addExportTask(multiSheetLog);
        // 添加多文件导出任务
        ExcelLog multiFileLog = createExcelLog(ExcelTypeEnum.MULTI_FILE);
        excelExportQueue.addExportTask(multiFileLog);
        // 添加动态表头导出任务
        ExcelLog dynamicHeadLog = createExcelLog(ExcelTypeEnum.DYNAMIC_HEAD);
        excelExportQueue.addExportTask(dynamicHeadLog);
    }
    
    /**
     * 创建ExcelLog实例的辅助方法
     * 
     * @param type Excel类型编码
     * @return ExcelLog实例
     */
    private ExcelLog createExcelLog(ExcelTypeEnum type) {
        ExcelLog excelLog = new ExcelLog();
        excelLog.setId(UUID.randomUUID().toString());
        excelLog.setType(type.getCode());
        excelLog.setCreateBy("admin");
        excelLog.setCreateTime(LocalDateTime.now());
        excelLog.setUpdateTime(LocalDateTime.now());
        excelLog.setParam("{}");
        return excelLog;
    }

}